//
//  OmniProfitCaculator_00116501.js
//  SugLisSystem
//
//  Created by Mr.Right on 2013-05-13.
//  Copyright 2013 Mr.Right. All rights reserved.
//

var omnipotence = require('corecenter/Omnipotence');
var pubFun = require('untils/PubFun');

function OmniProfitCaculator(lsPolData, mainPolData, headData) {
	this.mainPolData = mainPolData;
	this.lsPolData = lsPolData;
	this.headData = headData;
	this.douAryOmniData = [];
	var omniData = new omnipotence(lsPolData);
	this.addPrem = omniData.addPrem;
	this.partDraw = omniData.partDraw;
	this.tackAmntChange = omniData.tackAmntChange;
	this.suFluityInvestMent = omniData.suFluityInvestMent;
	this.initSQLData();
	this.calData();
}

/**
 * 初始化SQL语句的数据
 * */
OmniProfitCaculator.prototype.initSQLData = function() {
	var mainPolData = this.mainPolData;

	var SQL_W = "select scj * " + mainPolData['amnt'] + "/(case when '" + mainPolData['riskcode'] + "' = '00111000' then 10000 else 1000 end) From it_" + mainPolData['riskcode'] + " where age=" + mainPolData['insuredappage'] + " And  sex=" + mainPolData['insuredsex'] + " And insuyear=" + mainPolData['insuyear'] + " And payendyear=" + mainPolData['payendyear'] + " And payintv=" + mainPolData['payintv'] + " And payendyearflag='" + mainPolData['payendyearflag'] + "' And insuyearflag='" + mainPolData['insuyearflag'] + "' Order by bdnd";
	Ti.API.warn('[SProfitCaculator00116500]SQL_W:' + SQL_W);
	this.epcw = exeSQL.execSQL(SQL_W, exeSQL.TYPE_ARRAY);

	var SQL_L = "select ((case when '" + mainPolData['riskcode'] + "' = '00111000'  then 0 else dndhld end) + (case  when '" + mainPolData['riskcode'] + "' = '00112200' then GZJYJ + DXJYJ + HJCYJ + ZSBXJ else XJLQ_D end)) * " + mainPolData['amnt'] + "/(case when '" + mainPolData['riskcode'] + "' = '00111000' then 10000 else 1000 end) From it_" + mainPolData['riskcode'] + " where age = " + mainPolData['insuredappage'] + " And sex = " + mainPolData['insuredsex'] + " And insuyear = " + mainPolData['insuyear'] + " And payendyear = " + mainPolData['payendyear'] + " And payintv = " + mainPolData['payintv'] + " And payendyearflag = '" + mainPolData['payendyearflag'] + "' And insuyearflag = '" + mainPolData['insuyearflag'] + "' Order by bdnd";
	// Ti.API.warn('[SProfitCaculator00116500]SQL_L:' + SQL_L);
	this.epcl = exeSQL.execSQL(SQL_L, exeSQL.TYPE_ARRAY);

	var SQL_M = "select ((case when '" + mainPolData['riskcode'] + "' = '00111000'  then 0 else dndhlz end) + (case  when '" + mainPolData['riskcode'] + "' = '00112200' then GZJYJ + DXJYJ + HJCYJ + ZSBXJ else XJLQ_Z end)) * " + mainPolData['amnt'] + "/(case when '" + mainPolData['riskcode'] + "' = '00111000' then 10000 else 1000 end) From it_" + mainPolData['riskcode'] + " where age = " + mainPolData['insuredappage'] + " And sex = " + mainPolData['insuredsex'] + " And insuyear = " + mainPolData['insuyear'] + " And payendyear = " + mainPolData['payendyear'] + " And payintv = " + mainPolData['payintv'] + " And payendyearflag = '" + mainPolData['payendyearflag'] + "' And insuyearflag = '" + mainPolData['insuyearflag'] + "' Order by bdnd";
	// Ti.API.warn('[SProfitCaculator00116500]SQL_M:' + SQL_M);
	this.epcm = exeSQL.execSQL(SQL_M, exeSQL.TYPE_ARRAY);

	var SQL_H = "select ((case when '" + mainPolData['riskcode'] + "' = '00111000'  then 0 else dndhlg end) + (case  when '" + mainPolData['riskcode'] + "' = '00112200' then GZJYJ + DXJYJ + HJCYJ + ZSBXJ else XJLQ_G end)) * " + mainPolData['amnt'] + "/(case when '" + mainPolData['riskcode'] + "' = '00111000' then 10000 else 1000 end) From it_" + mainPolData['riskcode'] + " where age = " + mainPolData['insuredappage'] + " And sex = " + mainPolData['insuredsex'] + " And insuyear = " + mainPolData['insuyear'] + " And payendyear = " + mainPolData['payendyear'] + " And payintv = " + mainPolData['payintv'] + " And payendyearflag = '" + mainPolData['payendyearflag'] + "' And insuyearflag = '" + mainPolData['insuyearflag'] + "' Order by bdnd";
	// Ti.API.warn('[SProfitCaculator00116500]SQL_H:' + SQL_H);
	this.epch = exeSQL.execSQL(SQL_H, exeSQL.TYPE_ARRAY);
}
/**
 * 计算该险种的收益
 * */
OmniProfitCaculator.prototype.calData = function() {
	var maxIndex = this.headData.length;
	var years = this.mainPolData['years'];
	for (var i = 0; i < years; i++) {
		if (this.douAryOmniData[i] == null) {
			this.douAryOmniData[i] = [];
		}
		for (var j = 0; j < maxIndex; j++) {
			if (j == 0) {
				// 计算保单年度
				this.calYears(i, j);
			} else if (j == 1) {
				//TODO 结算利率-部分领取
				this.calPartDraw(i, j);
			} else if (j == 2) {
				// 结算利率-进入个人账户价值
				this.calEnterPersonCrash(i, j, 'W');
			} else if (j == 3) {
				// 结算利率-个人账户价值
				this.calIndividualValue(i, j);
			} else if (j == 4) {
				// 结算利率-身故保险金
				this.calDieValue(i, j);
			} else if (j == 5) {
				// 结算利率-满期保险金
				this.calFullYearPrem(i, j);
			} else if (j == 6) {
				//TODO 结算利率(低)-部分领取
				this.calPartDraw(i, j);
			} else if (j == 7) {
				// 结算利率(低)-进入个人账户价值
				this.calEnterPersonCrash(i, j, 'D');
			} else if (j == 8) {
				// 结算利率(低)-个人账户价值
				this.calIndividualValue(i, j);
			} else if (j == 9) {
				// 结算利率(低)-身故保险金
				this.calDieValue(i, j);
			} else if (j == 10) {
				// 结算利率(低)-满期保险金
				this.calFullYearPrem(i, j);
			} else if (j == 11) {
				//TODO 结算利率(中)-部分领取
				this.calPartDraw(i, j);
			} else if (j == 12) {
				// 结算利率(中)-进入个人账户价值
				this.calEnterPersonCrash(i, j, 'Z');
			} else if (j == 13) {
				// 结算利率(中)-个人账户价值
				this.calIndividualValue(i, j);
			} else if (j == 14) {
				// 结算利率(中)-身故保险金
				this.calDieValue(i, j);
			} else if (j == 15) {
				// 结算利率(中)-满期保险金
				this.calFullYearPrem(i, j);
			} else if (j == 16) {
				//TODO 结算利率(高)-部分领取
				this.calPartDraw(i, j);
			} else if (j == 17) {
				// 结算利率(高)-进入个人账户价值
				this.calEnterPersonCrash(i, j, 'G');
			} else if (j == 18) {
				// 结算利率(高)-个人账户价值
				this.calIndividualValue(i, j);
			} else if (j == 19) {
				// 结算利率(高)-身故保险金
				this.calDieValue(i, j);
			} else if (j == 20) {
				// 结算利率(高)-满期保险金
				this.calFullYearPrem(i, j);
			}
		}
	}
}
/**
 * 计算保单年度
 * */
OmniProfitCaculator.prototype.calYears = function(i, j) {
	this.douAryOmniData[i][j] = i + 1;
}
/**
 * 求出部分领取
 * */
OmniProfitCaculator.prototype.calPartDraw = function(i, j) {
	this.setAddPrem(i, j, 1);
}
/**
 * 计算进入个人账户价值
 * @param type 账户价值类型，D,Z,G(低，中，高)
 * */
OmniProfitCaculator.prototype.calEnterPersonCrash = function(i, j, type) {
	if (type == 'D') {
		if (i > this.epcl.length) {
			this.douAryOmniData[i][j] = 0;
		} else {
			this.douAryOmniData[i][j] = parseFloat(this.epcl[i][0]);
		}
	} else if (type == 'Z') {
		if (i > this.epcm.length) {
			this.douAryOmniData[i][j] = 0;
		} else {
			this.douAryOmniData[i][j] = parseFloat(this.epcm[i][0]);
		}
	} else if (type == 'G') {
		if (i > this.epch.length) {
			this.douAryOmniData[i][j] = 0;
		} else {
			this.douAryOmniData[i][j] = parseFloat(this.epch[i][0]);
		}
	} else if (type == 'W') {
		if (i > this.epcw.length) {
			this.douAryOmniData[i][j] = 0;
		} else {
			this.douAryOmniData[i][j] = parseFloat(this.epcw[i][0]);
		}
	}
}
/**
 * 求出对应的进入个人账户价值低中高并存入对应的位置
 * */
OmniProfitCaculator.prototype.calIndividualValue = function(i, j) {
	var rate = 0;
	if (j == 3) {
		rate = 0.025;
	}
	if (j == 8) {
		rate = 0.025;
	}
	if (j == 13) {
		rate = 0.045;
	}
	if (j == 18) {
		rate = 0.06;
	}
	this.calPartIndividualValue(i, j, rate);
}
/**
 * 计算身故保险金
 * */
OmniProfitCaculator.prototype.calDieValue = function(i, j) {
	this.douAryOmniData[i][j] = this.douAryOmniData[i][j - 1] * 1.05;
}
/**
 * 计算满期保险金
 * */
OmniProfitCaculator.prototype.calFullYearPrem = function(i, j) {
	if (i == (this.mainPolData['years'] - 1)) {
		this.douAryOmniData[i][j] = this.douAryOmniData[i][j - 2];
	} else {
		this.douAryOmniData[i][j] = 0;
	}

}
/**
 * 这是个根据传入的参数计算
 * @param type
 *            追加 0 部分领取 1 保额变更 2 副险保额变更3
 */
OmniProfitCaculator.prototype.setAddPrem = function(m, n, type) {
	var douNowYear = m + 1;
	var middSsrs;
	if (type == 0) {
		middSsrs = this.addPrem;
	} else if (type == 1) {
		middSsrs = this.partDraw;
	} else if (type == 2) {
		middSsrs = this.amntChange;
	} else if (type == 3) {
		middSsrs = this.tackAmntChange;
	} else if (type == 4) {
		middSsrs = this.suFluityInvestMent;
	}
	if (middSsrs == null) {
		return;
	}

	var tempData = 0;

	for (var i = 0; i < middSsrs.length; i++) {
		var intAddPremYear = parseInt(middSsrs[i][4]);
		var intAddPremEndYear = parseInt(middSsrs[i][5]);
		/** 因为追加保费有增减 */
		if (douNowYear >= intAddPremYear && douNowYear <= intAddPremEndYear) {
			var douAddPremValue = parseFloat(middSsrs[i][6]);
			tempData += douAddPremValue;
		}
	}
	this.douAryOmniData[m][n] = tempData;
}

OmniProfitCaculator.prototype.calPartIndividualValue = function(i, j, rate) {
	var douAryData = this.douAryOmniData;
	var douIndividualValue = 0;
	var douIndividualValue1 = 0;
	if (i == 0) {
		douIndividualValue1 = (-douAryData[i][j - 2]) * (1 + rate) + douAryData[i][j - 1];
		if (douIndividualValue1 < 0) {
			douIndividualValue = douAryData[i][j - 1];
			this.douAryOmniData[i][j - 2] = 0;
		} else {
			douIndividualValue = douIndividualValue1;
		}
	} else {
		douIndividualValue1 = (douAryData[i - 1][j] - douAryData[i][j - 2]) * (1 + rate) + douAryData[i][j - 1];
		if (douIndividualValue1 < 0) {
			douIndividualValue = (douAryData[i - 1][j]) * (1 + rate) + douAryData[i][j - 1];
			this.douAryOmniData[i][j - 2] = 0;
		} else {
			douIndividualValue = (douAryData[i - 1][j] - douAryData[i][j - 2]) * (1 + rate) + douAryData[i][j - 1];
		}
	}
	/** 对当前对应的个人帐户价值进行赋值 */
	this.douAryOmniData[i][j] = douIndividualValue;
}
/**
 * 重新定义和排序
 * */
OmniProfitCaculator.prototype.reOrderData = function() {
	var returnData = {};
	for (var i = 0; i < this.headData.length; i++) {
		var headFlag = this.headData[i];
		returnData[headFlag] = [];
		for (var j = 0; j < this.mainPolData.years; j++) {
			//保留两位小数
			returnData[headFlag].push(pubFun.round(this.douAryOmniData[j][i], 0));
			// returnData[headFlag].push(this.douAryOmniData[j][i]);
		}
		Ti.API.warn('[OmniProfitCaculator]00116501_' + headFlag + ':' + returnData[headFlag].toString());
	}
	return returnData;
}

module.exports = OmniProfitCaculator;
